package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.GoodsBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/3/27 0027
 * description：
 */
public class ShopContentViewHolder extends RecyclerView.ViewHolder {


//    @BindView(R.id.tv_goods_title_right)
//    TextView tvGoodsTitleRight;
    @BindView(R.id.tv_shop_goods_name)
    TextView tvShopGoodsName;
    @BindView(R.id.iv_shop_picture)
    ImageView ivShopPicture;
    @BindView(R.id.tv_shop_month_sale)
    TextView tvShopMonthSale;
    @BindView(R.id.tv_shop_high_praise)
    TextView tvShopHighPraise;
    @BindView(R.id.tv_shop_goods_price)
    TextView tvShopGoodsPrice;
    @BindView(R.id.tv_shop_goods_price_old)
    TextView tvShopGoodsPriceOld;
    @BindView(R.id.tv_goods_add)
    TextView tvGoodsAdd;
    @BindView(R.id.tv_goods_add_bg)
    TextView tvGoodsAddBg;
   @BindView(R.id.tv_goods_minus_bg)
    TextView tvGoodsMinusBg;
    @BindView(R.id.tv_goods_minus)
    TextView tvGoodsMinus;
    @BindView(R.id.tv_goods_num)
    TextView tvGoodsNum;
 @BindView(R.id.tv_good_mian)
    TextView tvGoodMian;


    private Context mContext;
    private GoodActionCallback callback;

    public ShopContentViewHolder(Context mContext, View itemView, GoodActionCallback callback) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.mContext = mContext;
        this.callback = callback;
    }


    public void setData(ShopGoodListBeen.DataBean.GoodsInfoBean goodsBeen, final int position,String shopID) {
//        if (goodsBeen.isIs_title()) {
//            tvGoodsTitleRight.setVisibility(View.VISIBLE);
//            tvGoodsTitleRight.setText(goodsBeen.getCategory_name());
//        } else {
//            tvGoodsTitleRight.setVisibility(View.GONE);
//        }
        tvShopGoodsName.setText(goodsBeen.getTitle());
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(goodsBeen.getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivShopPicture);

        tvShopMonthSale.setText("销量"+goodsBeen.getSale_num());
        tvShopHighPraise.setText("好评"+goodsBeen.getFavorable_comments());
        if (Double.parseDouble(goodsBeen.getDiscountprice()) == 0) {
            tvShopGoodsPrice.setText("¥"+goodsBeen.getMarketprice());
//            tvShopGoodsPriceOld.setText(goodsBeen.goodsPriceOld);
            tvShopGoodsPriceOld.setVisibility(View.INVISIBLE);
        } else {
            tvShopGoodsPriceOld.setVisibility(View.VISIBLE);
            tvShopGoodsPrice.setText("¥"+goodsBeen.getDiscountprice());
            tvShopGoodsPriceOld.setText("¥"+goodsBeen.getMarketprice());
        }
        tvShopGoodsPriceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        tvGoodsNum.setText(goodsBeen.getGood_sum() + "");
        if (goodsBeen.getGood_sum() == 0) {
            tvGoodsMinus.setVisibility(View.INVISIBLE);
            tvGoodsMinusBg.setVisibility(View.INVISIBLE);
            tvGoodsNum.setVisibility(View.INVISIBLE);
        } else {
            tvGoodsMinus.setVisibility(View.VISIBLE);
            tvGoodsMinusBg.setVisibility(View.VISIBLE);
            tvGoodsNum.setVisibility(View.VISIBLE);
        }
        tvGoodsAddBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }
//                tvGoodsMinus.setVisibility(View.VISIBLE);
//                tvGoodsNum.setVisibility(View.VISIBLE);
//                goodsBeen.setGood_sum(goodsBeen.getGood_sum()+1);
//                tvGoodsNum.setText(goodsBeen.getGood_sum() + "");
                callback.addAction(tvGoodsAdd, position, true);

            }}
        });
        tvGoodsMinusBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }

                callback.minusGood(position,true);
//                goodsBeen.setGood_sum(goodsBeen.getGood_sum()-1);
//                if (goodsBeen.getGood_sum() == 0) {
//                    tvGoodsMinus.setVisibility(View.INVISIBLE);
//                    tvGoodsNum.setVisibility(View.INVISIBLE);
//                }
//                tvGoodsNum.setText(goodsBeen.getGood_sum() + "");
            }}
        });


        if (UtilsData.getPreference(mContext,"activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(shopID)){
            tvGoodMian.setVisibility(View.VISIBLE);
        }else {
            tvGoodMian.setVisibility(View.GONE);
        }
    }


}





